package com.epalmpay.controller.channel.datareport;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.datareport.IWithdrawDataReportService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/** 
 * 说明：数据报表 :营销数据列表
 * 创建人：gwn
 * 创建时间：2017-06-15
 */
@Controller
@RequestMapping(value="/merchant/withdraw")
public class WithdrawController extends BaseController{
	
	@Resource
	private IWithdrawDataReportService withdrawDataReportService;
	
	/**
	 * 显示提现对账数据列表
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public List  list(){
		Map map = new HashMap();
		map.put("groupId",getRequest().getParameter("groupid"));
		map.put("searchkey",getRequest().getParameter("searchkey"));
		map.put("agentId", "null".equals(getRequest().getParameter("agentId"))?null:getRequest().getParameter("agentId"));
		map.put("shopId", "null".equals(getRequest().getParameter("shopId"))?null:getRequest().getParameter("shopId"));
		List result = new ArrayList();
		try {
			result = withdrawDataReportService.getAccountMonthList(map);
		} catch (ApplicationException ae) {
			result = null;
			ae.printStackTrace();
		} catch (Exception e) {
			result = null;
			e.printStackTrace();
		}
		System.out.println(result);
		return result;
	}

	/**
	 * 显示提现对账数据列表
	 * @param
	 * @return
	 */
	@RequestMapping(value="/memberlist")
	@ResponseBody
	public List memberlist(){
		Map map = new HashMap();
		map.put("groupId",getRequest().getParameter("groupid"));
		map.put("searchkey",getRequest().getParameter("searchkey"));
		List result = new ArrayList();
		try {
			result = withdrawDataReportService.getMemberMonthList(map);
		} catch (ApplicationException ae) {
			result = null;
			ae.printStackTrace();
		} catch (Exception e) {
			result = null;
			e.printStackTrace();
		}
		System.out.println(result);
		return result;
	}

	/**
	 * 显示提现对账日明细数据列表
	 * @param
	 * @return
	 */
	@RequestMapping(value="/daylist")
	@ResponseBody
	public List daylist(){
		Map map = new HashMap();
		map.put("groupId",getRequest().getParameter("groupid"));
		map.put("searchkey",getRequest().getParameter("searchkey"));
		List result = new ArrayList();
		List<Map<String, Object>>  memberresult = null;
		List<Map<String, Object>> dlresult = null;
		try {
			dlresult = withdrawDataReportService.getAccountDayList(map);
			memberresult = withdrawDataReportService.getMemberDayList(map);
			//处理商户和代理商日明细
			for(Map m : dlresult) {
				Map rp = new HashMap();
				if(m.get("agent_id") == null) {
					rp.put("withdrawer", m.get("shop_name").toString());
					rp.put("wdertype", "商户");
				} else if(m.get("shop_id") == null) {
					rp.put("withdrawer", m.get("agent_name").toString());
					rp.put("wdertype", "代理商");
				}
				rp.put("datet", m.get("gmt_modified").toString());
				rp.put("wdnumber", m.get("apply_amount").toString());
				result.add(rp);
			}
			//处理用户日明细
			for(Map m : memberresult) {
				Map rp = new HashMap();
				rp.put("withdrawer", m.get("payer_name").toString());
				rp.put("wdertype", "用户");
				rp.put("datet", m.get("gmt_modified").toString());
				rp.put("wdnumber", m.get("real_amount").toString());
				result.add(rp);
			}

			//对结果按日期进行排序
			Collections.sort(result, new Comparator<Map>() {

				@Override
				public int compare(Map o1, Map o2) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String date1 = o1.get("datet").toString();
					String date2 = o2.get("datet").toString();

					int i = 0;
					try {
						i = (int)(sdf.parse(date2).getTime() - sdf.parse(date1).getTime());
					} catch (Exception e) {
						e.printStackTrace();
					}
					return i;
				}
			});

		} catch (ApplicationException ae) {
			result = null;
			ae.printStackTrace();
		} catch (Exception e) {
			result = null;
			e.printStackTrace();
		}
		System.out.println(result);
		return result;
	}

	/**
	 * 显示提现对账月明细数据列表
	 * @param
	 * @return
	 */
	@RequestMapping(value="/monthlist")
	@ResponseBody
	public List monthlist(){
		Map map = new HashMap();
		map.put("groupId",getRequest().getParameter("groupid"));
		map.put("searchkey",getRequest().getParameter("searchkey"));
		List result = new ArrayList();
		List<Map<String, Object>> memberresult = null;
		List<Map<String, Object>> dlresult = null;
		try {
			dlresult = withdrawDataReportService.getAccountList(map);
			memberresult = withdrawDataReportService.getMemberDetailMonthList(map);
			//处理商户和代理商日明细
			for(Map m : dlresult) {
				Map rp = new HashMap();
				if(m.get("agent_id") == null) {
					rp.put("withdrawer", m.get("shop_name").toString());
					rp.put("wdertype", "商户");
				} else if(m.get("shop_id") == null) {
					rp.put("withdrawer", m.get("agent_name").toString());
					rp.put("wdertype", "代理商");
				}
				rp.put("datet", m.get("gmt_modified").toString());
				rp.put("wdnumber", m.get("apply_amount").toString());
				result.add(rp);
			}
			//处理用户日明细
			for(Map m : memberresult) {
				Map rp = new HashMap();
				rp.put("withdrawer", m.get("payer_name").toString());
				rp.put("wdertype", "用户");
				rp.put("datet", m.get("gmt_modified").toString());
				rp.put("wdnumber", m.get("real_amount").toString());
				result.add(rp);
			}

			//对结果按日期进行排序
			Collections.sort(result, new Comparator<Map>() {

				@Override
				public int compare(Map o1, Map o2) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String date1 = o1.get("datet").toString();
					String date2 = o2.get("datet").toString();

					int i = 0;
					try {
						i = (int)(sdf.parse(date2).getTime() - sdf.parse(date1).getTime());
					} catch (Exception e) {
						e.printStackTrace();
					}
					return i;
				}
			});

		} catch (ApplicationException ae) {
			result = null;
			ae.printStackTrace();
		} catch (Exception e) {
			result = null;
			e.printStackTrace();
		}
		System.out.println(result);
		return result;
	}
}
